<template>
  <div class="container">
    <div class="exam-base" v-if="!$util.isEmpty(recordData)">
      <div class="title">{{ recordData.paper_title }}</div>
      <div class="content">
        <div class="left">
          <div class="progress">
            <el-progress
              type="circle"
              :color="globThemeColor"
              width="150"
              stroke-width="10"
              :show-text="false"
              :percentage="percentage"
            ></el-progress>
            <div class="progress-box">
              <div class="score">
                <span class="big">{{ recordData.user_score }}</span>
                <span class="mini">分</span>
              </div>
              <div class="total">
                {{ `总分${recordData.paper_score}分` }}
              </div>
            </div>
          </div>
          <div class="info" v-if="recordData.answer_status == 1">
            *该试卷含有主观题，请等待阅卷完成
          </div>
        </div>
        <div class="line"></div>
        <div class="right">
          <div class="time">
            <span class="balel">用时</span>
            <span class="num">{{ recordData.time_takes | formatSeconds }}</span>
          </div>
          <div class="time">
            <span class="balel">交卷时间</span>
            <span class="num">{{ recordData.commit_time | parseTime }}</span>
          </div>
          <div class="footer">
            <div
              class="btn primary"
              @click="
                clickJump('/exam/paper/parsing/topic', {
                  id: recordData.id,
                  type: $route.query.type,
                  status: 2
                })
              "
            >
              错题解析
            </div>
            <div
              class="btn plain"
              @click="
                clickJump('/exam/paper/parsing/topic', {
                  id: recordData.id,
                  type: $route.query.type,
                  status: 1
                })
              "
            >
              全部解析
            </div>
            <div
              class="btn plain"
              @click="
                clickJump('/exam/paper/parsing/wrong', {
                  id: recordData.id,
                  code: recordData.unique_code
                })
              "
            >
              错题重练
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="exam-sheet">
      <div class="sheet-header">
        <div class="title">答题卡</div>
        <div class="example">
          <span class="box correct"></span>
          <span class="text">正确</span>
          <span class="box error"></span>
          <span class="text">错误</span>
          <span class="box default"></span>
          <span class="text">未作答</span>
          <span class="box ban"></span>
          <span class="text">主观题</span>
        </div>
      </div>
      <div class="sheet-body" v-if="!$util.isEmpty(recordList)">
        <div class="parts" v-for="(parts, j) in recordList" :key="parts.id">
          <div class="title">{{ parts.title }}</div>
          <div class="list">
            <div class="item" v-for="(item, i) in parts.value" :key="item.id">
              <div
                class="span"
                :class="classStatus(item.answered, item.answer_right, item.subjective)"
              >
                {{ addNum(j) + (i + 1) }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
/** 累加器 **/
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0)
export default {
  data() {
    return {
      recordData: {},
      recordList: []
    }
  },
  computed: {
    ...mapGetters({
      globThemeColor: 'projectSetting/getGlobThemeColor'
    }),
    percentage() {
      let num = Math.floor((this.recordData.user_score / this.recordData.paper_score) * 100)
      return num < 0 ? 0 : num > 100 ? 100 : num
    }
  },
  created() {
    this.getExamRecord()
  },
  methods: {
    getExamRecord() {
      this.$api.getUserExamRecord(
        {
          record_id: this.$route.query.id
        },
        res => {
          this.recordData = res.data
          this.recordList = res.data.paper_parts
        }
      )
    },
    // 计算答题卡试题编号
    addNum(i) {
      let numArr = this.recordList.map(item => item.value.length)
      let newArr = numArr.slice(0, i)
      return sum(newArr)
    },
    classStatus(answered, answer_right, subjective) {
      // correct正确 error错误 default未答 ban主观题
      if (subjective == 1) return 'ban'
      if (answered == 1) return answer_right == 1 ? 'correct' : 'error'
      return 'default'
    },
    clickJump(url, query) {
      this.$router.push({
        path: url,
        query: query
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.exam-base {
  margin-top: 10px;
  background-color: #ffffff;
  .title {
    padding: 14px 23px;
    font-size: 15px;
    font-weight: 500;
    color: $text-color-import;
    border-bottom: 1px solid $border-color-divider;
  }
  .content {
    display: flex;
    align-items: center;
    margin-top: 46px;
    padding-bottom: 24px;
    .left {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      .progress {
        position: relative;
        .progress-box {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .score {
            .big {
              font-size: 42px;
              font-weight: bold;
              @include edu-color-auxiliary;
            }
            .mini {
              font-size: 14px;
              font-weight: 400;
              @include edu-color-auxiliary;
            }
          }
          .total {
            font-size: 14px;
            font-weight: 400;
            color: $text-color-secondary;
          }
        }
      }
      .info {
        margin-top: 16px;
        font-size: 12px;
        font-weight: 400;
        color: $text-color-auxiliary;
      }
    }
    .line {
      height: 160px;
      border-left: 1px solid $border-color-divider;
    }
    .right {
      flex: 1;
      margin-left: 140px;
      .time {
        display: flex;
        & + .time {
          margin-top: 40px;
        }
        .balel {
          width: 90px;
          font-size: 14px;
          font-weight: 400;
          color: $text-color-secondary;
        }
        .num {
          font-size: 14px;
          font-weight: 400;
          color: $text-color-import;
        }
      }
      .footer {
        display: flex;
        margin-top: 42px;
      }
      .btn {
        width: 94px;
        height: 32px;
        line-height: 30px;
        text-align: center;
        border-radius: 4px;
        border-width: 1px;
        border-style: solid;
        font-size: 14px;
        font-weight: 400;
        cursor: pointer;
        box-sizing: border-box;
        & + .btn {
          margin-left: 10px;
        }
        &.primary {
          color: #ffffff;
          @include edu-color-primary(background-color);
          @include edu-color-primary(border-color);
        }
        &.plain {
          @include edu-color-auxiliary;
          @include edu-color-primary(border-color);
          &:hover {
            color: #ffffff;
            @include edu-color-primary(background-color);
          }
        }
      }
    }
  }
}
.exam-sheet {
  margin-top: 10px;
  background-color: #ffffff;
  .sheet-header {
    padding: 14px 23px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid $border-color-divider;
    .title {
      font-size: 15px;
      font-weight: 500;
      color: $text-color-import;
    }
    .example {
      display: flex;
      align-items: center;
      .box {
        width: 16px;
        height: 16px;
        border-radius: 4px;
        margin-left: 16px;
        margin-right: 4px;
        box-sizing: border-box;
        &.correct {
          background-color: $text-color-free;
        }
        &.error {
          background-color: $text-color-error;
        }
        &.default {
          border: 1px solid #b7bac1;
        }
        &.ban {
          background-color: #ededed;
          border: 1px solid #b7bac1;
        }
      }
      .text {
        font-size: 13px;
        font-weight: 400;
        color: $text-color-import;
      }
    }
  }
  .sheet-body {
    padding: 0 23px 36px;
    .parts {
      margin-top: 24px;
      .title {
        font-size: 14px;
        font-weight: 400;
        color: $text-color-import;
      }
      .list {
        display: flex;
        flex-wrap: wrap;
        margin-left: -7px;
        margin-right: -7px;
        .item {
          padding-left: 7px;
          padding-right: 7px;
          margin-top: 14px;
          .span {
            width: 28px;
            height: 28px;
            font-size: 14px;
            font-weight: 400;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-sizing: border-box;
            &.correct {
              color: #ffffff;
              background-color: $text-color-free;
            }
            &.error {
              color: #ffffff;
              background-color: $text-color-error;
            }
            &.default {
              color: $text-color-auxiliary;
              border: 1px solid #b7bac1;
            }
            &.ban {
              color: $text-color-auxiliary;
              background-color: #ededed;
              border: 1px solid #b7bac1;
            }
          }
        }
      }
    }
  }
}
</style>
